package four;

public class A063 {
    public static void main(String[] args) {
        int[] a = {5,7,7,8,8,10};
        //searchRange(a,8);
    }

    /**
     * 34.
     * @param nums
     * @param target
     * @return
     */

    public int[] searchRange(int[] nums, int target) {
        int[] ret = new int[]{-1,-1};
        int n = nums.length;
        int left=0,right=n-1;
        //1.寻找左端点 mid以及左边都是小于target的，而右边都是>=
        while(left<right) {
            int mid = (right+left)/2;
            if(nums[mid]<target) left=mid+1;
            else right=mid;
        }
        if(right>=0 && nums[left]==target) ret[0]=left;

        left=0;right=n-1;
        //2.寻找右端点（注意取中间值为第二个）
        while(left<right) {
            int mid = left+(right-left+1)/2;
            if(nums[mid]<=target) left=mid;
            else right=mid-1;
        }
        if(left<n && nums[right]==target) {
            ret[1]=right;
        }
        return ret;
    }
}
